import axiosIns from './axiosIns'
import timeout from './error.js'
import url from './config'
const errMsg = 'tomcat原地爆炸了'

export default {
  getLabelDatas (params) {
    return new Promise((resolve, reject) => {
      if (params) {
        axiosIns.get(`${url}/tags/listFirstTags/${params}/10`, {
          withCredentials: true
        })
        .then(data => {
          if (data.data.data.success === true) {
            resolve(data.data.data.data)
          } else {
            reject(data.data.data.message)
          }
        })
        .catch(() => {
          reject(errMsg)
        })
      } else {
        axiosIns.get(`${url}/tags/listFirstTags/0/0`)
        .then(data => {
          if (data.data.data.success === true) {
            resolve(data.data.data.data)
          } else {
            reject(data.data.data.message)
          }
        })
        .catch(() => {
          reject(errMsg)
        })
      }
      timeout(reject)
    })
  },
  deleteLabel (params) {
    return new Promise((resolve, reject) => {
      axiosIns.delete(`${url}/tags/delete/${params}`)
        .then(data => {
          if (data.data.data.success === true) {
            resolve()
          } else {
            reject(data.data.message)
          }
        })
        .catch(() => {
          reject(errMsg)
        })
      timeout(reject)
    })
  },
  addTags (params) {
    return new Promise((resolve, reject) => {
      axiosIns.post(`${url}/tags/add`, params)
        .then(data => {
          console.log('add', data)
          if (data.data.data.success === true) {
            resolve()
          } else {
            reject(data.data.data.message)
          }
        })
        .catch(() => {
          console.log('fail')
          reject(errMsg)
        })
      timeout(reject)
    })
  }
}
